package cn.tyf.controller;

import cn.tyf.common.ConstAutoConfig;
import cn.tyf.domain.*;
import cn.tyf.service.NoticeService;
import com.alibaba.excel.EasyExcel;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.Date;
import java.util.List;

/**
 * 公告
 */
@RequestMapping("/notice")
@Controller
public class NoticeController {
    @Autowired
    private NoticeService noticeService;

    @Autowired
    private ConstAutoConfig constAutoConfig;

    @Autowired
    private ResultInfo resultInfo;

    /**
     * 添加公告
     * @param notice
     * @param request
     * @return
     */
    @RequestMapping("/addNotice")
    @ResponseBody
    public Boolean add(Notice notice, HttpServletRequest request){
        try {
            Admin admin = (Admin) request.getSession().getAttribute(constAutoConfig.getUserLogin());
            notice.setUid(admin.getId());
            notice.setTime(new DateTime().toString("yyyy-MM-dd HH-mm-ss"));
            noticeService.addNotice(notice);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 分页查询公告
     * @param currentPage
     * @param pageSize
     * @return
     */
    @RequestMapping("/findByPage")
    @ResponseBody
    public ResultInfo findByPages(Integer currentPage, Integer pageSize){
        try {
            PageBean<Notice> noticePageBean = noticeService.findByPage(currentPage, pageSize);
            resultInfo.setData(noticePageBean);
            resultInfo.setFlag(true);
            return resultInfo;
        } catch (Exception e) {
            e.printStackTrace();
            resultInfo.setFlag(false);
            return resultInfo;
        }
    }

    /**
     * 根据nid查询一条公告
     * @param nid
     * @return
     */
    @RequestMapping("/findByNid")
    @ResponseBody
    public Notice findByNid(Integer nid){
        return noticeService.findByNotice(nid);
    }

    /**
     * 根据nid更新数据
     * @param notice
     */
    @RequestMapping("/updateNotice")
    @ResponseBody
    public void updateNotice(Notice notice){
        noticeService.updateNotice(notice);
    }

    /**
     * 根据nid删除公告
     * @param nid
     */
    @RequestMapping("/deleteByNid")
    @ResponseBody
    public void deleteByNid(Integer nid){
        noticeService.deleteByNid(nid);
    }

    /**
     * 根据nids 批量删除公告
     * @param checkbox
     */
    @RequestMapping("/deleteByNids")
    @ResponseBody
    public void deleteByNids(String[] checkbox){
        for (String nid : checkbox) {
            System.out.println("nid = " + nid);
            noticeService.deleteByNid(Integer.parseInt(nid));
        }
    }

    /**
     * 导出数据
     * @return
     */
    @RequestMapping("/noticeExcel")
    @ResponseBody
    public Boolean noticeExcel(){
        try {
            List<Notice> recordList = noticeService.findAll();
            String fileName = constAutoConfig.getExcelDowPath() + "/公告记录.xlsx";
            EasyExcel.write(fileName, Notice.class).sheet("公告记录").doWrite(recordList);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @RequestMapping("/downloadNotice")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        List<Notice> noticeList = noticeService.findAll();
        String fileName = URLEncoder.encode("公告记录表", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        EasyExcel.write(response.getOutputStream(), Notice.class).sheet("公告记录").doWrite(noticeList);
    }
}
